package j79;

import cn.hutool.core.thread.ThreadUtil;
import java.lang.ref.PhantomReference;
import java.lang.ref.ReferenceQueue;

/**
 * @author chengshuai
 * @date 2021/4/18 17:19
 */
public class PhantomReferenceDemo {

    public static void main(String[] args) {
        Object o1 = new Object();
        ReferenceQueue<Object> referenceQueue = new ReferenceQueue();
        PhantomReference<Object> phantomReference = new PhantomReference<>(o1, referenceQueue);

        System.out.println(o1);
        System.out.println(phantomReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println("========");
        o1 = null;
        System.gc();
        ThreadUtil.sleep(500);
        System.out.println(o1);

        System.out.println(phantomReference.get());
        System.out.println(referenceQueue.poll());
        System.out.println(referenceQueue.poll());
    }
}
